#include "MyEventHandler.h"

MyEventHandler::MyEventHandler()
	:isExiting	(false)
	,paused		(false)
{
	scene = new MyScene();

	CreateAnimation(); //creates an animation. 
}
MyEventHandler::~MyEventHandler()
{
	delete explosion; 
	delete explosionAnim; 

	delete scene;
}


void MyEventHandler::CreateAnimation() //creates an animation. 
{
	//Animations exist as "list of sub-images inside larger ones". 
	//Here we're using "explosion.tga", which is a 320 x 320 image, 
	//composed as a grid of 5 x 5 "sub-images", each being 64 x 64. 
	//to create an animation, we'll need: 
	//- the image data (a bitmap)
	//- a list of "sub-images"
	//- the number of "sub-images"
	//- the width and height of a single sub-image 
	//- how long each sub-image should be on screen. 
	//Two or more animations might share the same graphics data, of course; however, 
	//one animation can't be shared between multiple objects - each game object should have
	//its own animation. 

	//So, we'll start by creating the required data. 
	//we'll start with image data: 
	explosion = new Bitmap("data\\pacMan_an1.tga"); 

	//Next the indices. They are zero-based so that the sub-image at (x, y)
	//in the image data has index = (sub-images per row * y) + x, with x and y being zero-based.
	//For instance, the second sub-image (x=1) in the third row (y=2) of our test image would 
	//have index = (5 sub-images per row * 2) + 1 = 11
	//We won't be using frames 23 and 24, because the .tga doesn't have them. 
	//Any index in the list is fine, in any order, as long as it's valid. 
	int indices[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10/*, 11, 
		12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22*/}; 

	//we're having 23 indices, but let's leave the math to the pc. 
	int indicesCount = sizeof(indices) / sizeof(int); 

	//now we'll create the animation. The constructor requires: 
	//- the image data we created earlier, which will be needed as long as the animation is alive
	//- the width of a single sub-image (64)
	//- the height of a single sub-image (also 64) 
	//- the index list (it is not kept along with the animation) 
	//- the lenght of the index list
	//- the time between each sub-image, in seconds (but in floating point, ie: 0.1f means roughly one tenth of a second)
	explosionAnim = new Animation(explosion, 16, 16, indices, indicesCount, 0.15f); 
	//then, we'll start the animation. It will update itself on its own. 
	explosionAnim->Start(true);
}

void MyEventHandler::DrawScene(Bitmap* renderTarget) 
{
	scene->DrawTo(renderTarget, 0, 0);//30 , 10
};


void MyEventHandler::DrawAnimation(Bitmap* renderTarget)
{
	//setup is nasty but drawing is pretty easy, but we'll also move it around the screen. 
	//static int y = 100; //start at 400. 
	//static int my = 1; // start by moving 1 pixel per frame. 
	//y+=my;	//move x
	//if(y > 364) my = -1; //too much to the right; we'll change direction and move to the left
	//if(y < 101) my = 1; //too much to the left; we'll change direction and move to the right
	explosionAnim->Draw(renderTarget, 500, 360); //draws the current animation frame at (x, 100) on the screen
}



void MyEventHandler::OnRender(Bitmap* renderTarget)	//from base class. Renders the screen as needed. 
{
	//DrawAnimation(renderTarget);
	DrawScene(renderTarget); 

	if(!scene->isGameEnded())
	{
		if(isExiting){
			renderTarget->RenderText(500, 200, "Goodbye!!", 100, 125, 192);
			OutputDebugStringA("\tGoodbye and thanks for playing!!!\r\n");
		}

		if(paused)	{
			renderTarget->RenderText(500, 60, "Game PAUSED.", 192, 192, 192);
		}else {
			scene->updateLogic();
		}
	}else {
		char finalMsg[128];
		switch(scene->getGameState()){
		case GAME_OVER:
			sprintf(finalMsg, "GAME OVER");
			break;
		case LOST:
			sprintf(finalMsg, "PC WON!");
			break;
		case WON:
			sprintf(finalMsg, "YOU WIN!!");
		}
		renderTarget->RenderText(500, 260, finalMsg, 255, 50, 255);
	}
}

void MyEventHandler::OnInput(unsigned int Key, KeyStatus status) //from base class. Receives the input. 
{
	scene->keyInput(Key, status);
	//38 UP 37 LEFT 40 DOWN 39 RIGHT

	if(status == KeyPressed)
	{
		switch(Key)
		{
		case 0x1B://ESC
			//how can I quit the application?
			isExiting = true;
			break;
		case 0x50://P
		case 0x13://pause
		case 0x20://Space
			paused = !paused;
			break;
		}
	}	
}

void MyEventHandler::OnMouseInput(int MousePosX, int MousePosY, MouseKey key, KeyStatus status)
{
	//noOp();

	/*char buffer[256]; 
	sprintf(buffer, "Mouse input ( %i, %i) %s %s\n", MousePosX, MousePosY, 
		key == No_Key ? "No Key": (key == LMouse ? "LMouse": "RMouse"), 
		status == KeyPressed ? "pressed." : (status == MouseMove ? "moved." : "released.")); 
	OutputDebugStringA(buffer); */
}